<?php

class Cms_ProductController extends Henry_Controller_Action
{
	private $allLang;
    public function init()
    {
        parent::init();
        $this->allLang = Henry_Languages::getAllLang();
    }

    public function indexAction()
    {
    	$this->view->allLangJson = Zend_Json::encode($this->allLang);
    }
	public function addcategoriesAction()
    {
    	$currentLang = $this->_request->getParam('lg',Henry_Global::CMS_DEFAULT_LANG);
    	if (!in_array($currentLang, array_keys($this->allLang))) {
    		throw new Zend_Controller_Action_Exception("Khong Ton Tai",Henry_Error::NOT_EXISTS_LANG);
    	}
    	$catOb = new Henry_ProductCategories_Categories();
    	
    	if ($this->_request->isPost()){
    		$data = $this->_request->getParams();
    		$catOb->addCategories($data);
    	}
    	$this->view->catTree = $catOb->getCatTree($currentLang);
    	$this->view->defaultCMSLANG = $currentLang;
    }
    public function testAction()
    {
		$this->_helper->viewRenderer->setNoRender(true);
		$ct = new Henry_Contents();
		$data = $ct->checkExists('footer','vi');
		Henry_TestUtil::printNoDie($data);
    }
    //Update Product
    public function updateAction(){
    	
    }
	public function menuAction()
	{
		$thongtinchungMenu = array(
			array(
				'title' => 'Danh Mục Sản Phẩm',
				'link'  => array(
					'module'    =>'cms',
					'controller'=>'product',
					'action'    =>'index'
				)
			),
			array(
				'title' => 'Cập Nhập Sản Phẩm',
				'link'  => array(
					'module'    =>'cms',
					'controller'=>'product',
					'action'    =>'update',
				)
			)
		);
		$this->view->menu = $thongtinchungMenu;
	}
	
	//Lay du lieu mau
	public function getsampledataAction(){
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		$filename = $this->_request->getParam('filename','null.html');
		$filePath = WEB_BASE_URL.'/sampledata/product/categories/'.$filename.".html";
		$content = '';
		if (file_exists($filePath))
			$content = file_get_contents($filePath);
		echo $content;	
	}
	
	public function postDispatch(){
		$this->view->allLang = $this->allLang;
        $tmp = $this->view->getScriptPaths();
        $jsPath = str_replace($_SERVER['DOCUMENT_ROOT'], "", $tmp[0])."product/js";
        $jsPath = str_replace("\\", "/", $jsPath);
        $this->view->jsPath = $jsPath;
        $this->view->itemInPage = 20;
        $catOb = new Henry_ProductCategories_Categories();
        $this->view->totalItem = $catOb->getTotalRecord();
	} 
	
	//---------------------------------------------
	//Product Ajax Process
	public function getcatlistAction(){
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		
		$SortColumsArr = array(
			0=>'categories_title',//default Cloum Sort
			3=>'categories_id',
			4=>'categories_title',
			5=>'parent_cat_title',
			6=>'created',
			7=>'language'
		);
		
		
		$currentLang = $this->_request->getParam("language",Henry_Global::CMS_DEFAULT_LANG);
		$sSortDir_0 = $this->_request->getParam("sSortDir_0","asc");
		$iSortCol_0 = $this->_request->getParam("iSortCol_0",0);
		$iDisplayStart = $this->_request->getParam("iDisplayStart",null);
		$iDisplayLength = $this->_request->getParam("iDisplayLength",null); 
		$catOb = new Henry_ProductCategories_Categories();
		$iTotal  = $this->_request->getParam('totalItem',$catOb->getTotalRecord($currentLang));
		$data = $catOb->getCatelist($currentLang,array('categories_id','categories_title','created','categories_p0','categories_p1','categories_p2','categories_p3','language','parent_id'),null,$SortColumsArr[$iSortCol_0]." ".$sSortDir_0,$iDisplayLength,$iDisplayStart);
		$output = array(
			"sEcho" => intval($_GET['sEcho']),
			"iTotalRecords" => $iTotal,
			"iTotalDisplayRecords" => count($data),
			"aaData" => $data
		);
		echo Zend_Json::encode($output); 		
	}
}

